﻿Imports SIRData
Imports SIREntities
Public Class frmTomaDeInvetarioNuevo
  Private m_bNuevo As Boolean
  Private m_bListado As Boolean
  Private m_iNumInv As Int32 = 0
  Private m_cNumInv As String = String.Empty
  Private tbDetalleInventario As New DataTable

  Public Property bNuevo() As Boolean
    Get
      Return m_bNuevo
    End Get
    Set(ByVal value As Boolean)
      m_bNuevo = value
    End Set
  End Property

  Public Property bListado() As Boolean
    Get
      Return m_bListado
    End Get
    Set(ByVal value As Boolean)
      m_bListado = value
    End Set
  End Property

  Public Property oiNumInv() As Int32
    Get
      Return m_iNumInv
    End Get
    Set(ByVal value As Int32)
      m_iNumInv = value
    End Set
  End Property

  Public Property ocNumInv() As String
    Get
      Return m_cNumInv
    End Get
    Set(ByVal value As String)
      m_cNumInv = value
    End Set
  End Property

  Private Sub CrearEstructuraTabla()
    tbDetalleInventario.Columns.Add("iNumInv", Type.GetType("System.Int32"))
    tbDetalleInventario.Columns.Add("siItem", Type.GetType("System.Int16"))
    tbDetalleInventario.Columns.Add("siCodProd", Type.GetType("System.Int16"))
    tbDetalleInventario.Columns.Add("cCodProd", Type.GetType("System.String"))
    tbDetalleInventario.Columns.Add("vNomProd", Type.GetType("System.String"))
    tbDetalleInventario.Columns.Add("siCantidad", Type.GetType("System.Int16"))
    tbDetalleInventario.Columns.Add("tiUniMed", Type.GetType("System.Byte"))
    tbDetalleInventario.Columns.Add("vUniMed", Type.GetType("System.String"))
    tbDetalleInventario.Columns.Add("vObsInv", Type.GetType("System.String"))

  End Sub

  Private Sub CargarDetalleInventario()
    Dim m_InventarioDetalle As New clsInventarioDetalle(g_sConnectString)
    Try
      tbDetalleInventario = m_InventarioDetalle.InventarioDetalleListar(m_iNumInv)

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      m_InventarioDetalle = Nothing
      GC.Collect()
    End Try
  End Sub


  Private Sub frmInvetarioNuevo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
      Me.Icon = My.Resources.iconInventory

      Call CrearEstructuraTabla()

      If m_bNuevo Then
        cbovEstado.SelectedIndex = 2
        cbovEstado.Enabled = False
      Else
        Me.Text = ".:Editar Toma de Inventario:."
        Call CargarDetalleInventario()
      End If

      dgvDetalle.DataSource = tbDetalleInventario
   


    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnResponsable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResponsable.Click
    Dim oform As New frmEmpleadoListado
    Try
      oform.ShowDialog()

      If oform.bSelected Then
        lblvNomResp.Tag = oform.osiCodEmp
        lblvNomResp.Text = oform.ovNomEmp
      End If


    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnAlmacen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlmacen.Click
    Dim oform As New frmAlmacenListado
    Try
      oform.ShowDialog()

      If oform.bSelected Then
        lblvNomAlm.Tag = oform.osiCodAlm
        lblvNomAlm.Text = oform.ovNomAlm
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnOrdenar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrdenar.Click
    Try
      If dgvDetalle.RowCount = 0 Then
        MessageBox.Show("No se han encontrado registros para Ordenar", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      Else
        Call OrdenarDetalleDeInventario()
      End If


    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
    Dim oForm As New frmProductoAgregar
    Try
      oForm.oInventarioDetalle.siItem = dgvDetalle.Rows.Count + 1
      oForm.ShowDialog()

      If oForm.obSave Then
        AgregarDetalleDeInventario(oForm.oInventarioDetalle)
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnQuitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuitar.Click
    Try
      If dgvDetalle.RowCount = 0 Then
        MessageBox.Show("No se han encontrado registros para quitar", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      Else
        tbDetalleInventario.Rows.RemoveAt(dgvDetalle.CurrentCell.RowIndex)
        tbDetalleInventario.AcceptChanges()
        dgvDetalle.Refresh()
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvDetalleo_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvDetalle.RowPrePaint
    Try
      dgvDetalle.Rows(e.RowIndex).DefaultCellStyle.BackColor = IIf(e.RowIndex Mod 2 = 0, Color.Honeydew, Color.White)
      dgvDetalle.Rows(e.RowIndex).DefaultCellStyle.Font = New Font("Lucida Sans", 7)
    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Public Sub AgregarDetalleDeInventario(ByVal pDetalleInventario As clsInventarioDetalleBE)
    Dim oDataRow As DataRow = tbDetalleInventario.NewRow
    Try
      oDataRow("iNumInv") = pDetalleInventario.iNumInv
      oDataRow("siItem") = pDetalleInventario.siItem
      oDataRow("siCodProd") = pDetalleInventario.siCodProd
      oDataRow("cCodProd") = pDetalleInventario.cCodProd
      oDataRow("vNomProd") = pDetalleInventario.vNomProd
      oDataRow("siCantidad") = pDetalleInventario.siCantidad
      oDataRow("tiUniMed") = pDetalleInventario.tiUniMed
      oDataRow("vUniMed") = pDetalleInventario.vUniMed
      oDataRow("vObsInv") = pDetalleInventario.vObsInv

      tbDetalleInventario.Rows.Add(oDataRow)

      dgvDetalle.Refresh()

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvDetalle_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvDetalle.CellPainting
    Try
      If (e.ColumnIndex >= 0 And dgvDetalle.Columns(e.ColumnIndex).Name = "btnEditar" And e.RowIndex >= 0) Then
        e.Paint(e.CellBounds, DataGridViewPaintParts.All)
        Dim celBoton As DataGridViewButtonCell = dgvDetalle.Rows(e.RowIndex).Cells("btnEditar")
        Dim icoAgregar As Icon = My.Resources.iconEdit10x10

        celBoton.ToolTipText = "Editar"
        e.Graphics.DrawIcon(icoAgregar, e.CellBounds.Left + 10, e.CellBounds.Top + 6)

        dgvDetalle.Rows(e.RowIndex).Height = icoAgregar.Height + 12
        dgvDetalle.Columns(e.ColumnIndex).Width = icoAgregar.Width + 20

        e.Handled = True
      End If
    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvDetalle_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDetalle.CellClick
    Try

      If dgvDetalle.Columns(e.ColumnIndex).Name = "btnEditar" Then
        Me.Enabled = False

        Dim oForm As New frmDetalleInventarioAgregar

        oForm.bNuevo = False
        oForm.lblcCodProd.Text = dgvDetalle.Item("cCodProd", dgvDetalle.CurrentCell.RowIndex).Value
        oForm.lblvNomProd.Text = dgvDetalle.Item("vNomProd", dgvDetalle.CurrentCell.RowIndex).Value
        oForm.nudsiCantidad.Value = dgvDetalle.Item("siCantidad", dgvDetalle.CurrentCell.RowIndex).Value
        oForm.txtvObsInv.Text = dgvDetalle.Item("vObsInv", dgvDetalle.CurrentCell.RowIndex).Value
        oForm.ShowDialog()

        If oForm.obSave Then
          dgvDetalle.Item("siCantidad", dgvDetalle.CurrentCell.RowIndex).Value = oForm.osiCantidad
          dgvDetalle.Item("vObsInv", dgvDetalle.CurrentCell.RowIndex).Value = oForm.ovObsInv
        End If
        Me.Enabled = True
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub OrdenarDetalleDeInventario()
    Try
      For x As Integer = 0 To dgvDetalle.RowCount - 1
        dgvDetalle.Item("siItem", x).Value = x + 1
      Next
      dgvDetalle.Refresh()

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Function ValidarCampos() As Boolean
    Try
      ' Validar Responsable
      If lblvNomResp.Text.Trim.Length = 0 Then
        MessageBox.Show("Por favor debe seleccionar un responsable.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnResponsable.Focus()
        Exit Function
      End If

      ' Validar Almacen
      If lblvNomAlm.Text.Trim.Length = 0 Then
        MessageBox.Show("Por favor debe seleccionar un almacén.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnAlmacen.Focus()
        Exit Function
      End If

      If cbovEstado.SelectedIndex = 0 Then
        MessageBox.Show("Por favor debe seleccionar un estado para la Toma de Inventario.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        cbovEstado.DroppedDown = True
        cbovEstado.Focus()
      End If

      If tbDetalleInventario.Rows.Count = 0 Then
        MessageBox.Show("Por favor debe agregar al menos un registro.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        btnAgregar.Focus()
        Exit Function
      End If

      Return True

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      GC.Collect()
    End Try
  End Function

  Private Sub GuardarTomaDeInventario()
    Dim m_Inventario As New clsInventario(g_sConnectString)
    Try
      Me.Cursor = Cursors.WaitCursor

      If ValidarCampos() = False Then Exit Sub

      Me.Enabled = False ' Bloquer el Formulario para guardar los datos

      Call OrdenarDetalleDeInventario()

      If m_bNuevo Then
        lblcNumInv.Text = m_Inventario.InventarioInsertar(m_iNumInv, m_cNumInv, lblvNomAlm.Tag, lblvNomResp.Tag, oUsuario.iCodUsu, dtpsdFecDoc.Value, Microsoft.VisualBasic.Left(cbovEstado.Text, 1), tbDetalleInventario)
        MessageBox.Show("Se ha registrado correctamente la Toma de Inventario", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      Else
        m_Inventario.InventarioActualizar(m_iNumInv, lblvNomAlm.Tag, lblvNomResp.Tag, oUsuario.iCodUsu, dtpsdFecDoc.Value, Microsoft.VisualBasic.Left(cbovEstado.Text, 1), tbDetalleInventario)
        MessageBox.Show("Se ha actualizado correctamente la Toma de Inventario", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      End If

      gbDatos.Enabled = False
      gbDetalle.Enabled = False
      btnGuardar.Enabled = False

      Me.Enabled = True

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      Me.Cursor = Cursors.Default
      GC.Collect()
    End Try
  End Sub

  Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
    Try

      Call GuardarTomaDeInventario()

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCerrar.Click
    Me.Close()
  End Sub
End Class